﻿using MT.Log;
using System;
using System.Timers;
using System.Windows;
using System.Windows.Input;
using Timer = System.Timers.Timer;

namespace MT.CustomWindow
{
    public enum MessageBoxType
    {
        With_Ok = 0,
        With_OkCancel,
    }

    /// <summary>
    /// MessageBoxWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MessageBoxWindow : Window
    {
        public static Action<bool> WindowShowAction;

        private Timer windowCloseTimer = new Timer();
        private MessageBoxType messageBoxType = MessageBoxType.With_Ok;

        public MessageBoxWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if(messageBoxType == MessageBoxType.With_Ok)
                Button_Cancel.Visibility = Visibility.Collapsed;
        }

        public MessageBoxWindow(MessageBoxType type)
        {
            messageBoxType = type;
            InitializeComponent();
        }

        private void WindowCloseTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Application.Current.Dispatcher.Invoke(()=>
            {
                windowCloseTimer.Stop();
                this.DialogResult = false;
                this.Close();
                WindowShowAction?.Invoke(false);
            });
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void Button_OK_Click(object sender, RoutedEventArgs e)
        {
            if (windowCloseTimer.Enabled)
                windowCloseTimer.Stop();

            this.DialogResult = true;
            this.Close();
        }

        private void Btn_Close_Click(object sender, RoutedEventArgs e)
        {
            if (windowCloseTimer.Enabled)
                windowCloseTimer.Stop();

            this.DialogResult = false;
            this.Close();
        }

        private void SetHintMessage(string message, bool autoClose)
        {
            TextBox_HintMessage.Text = message;
            if (autoClose)
            {
                windowCloseTimer.Elapsed += WindowCloseTimer_Elapsed;
                windowCloseTimer.Interval = 5000;
                windowCloseTimer.Enabled = true;
                windowCloseTimer.Start();
            }  
        }

        public static bool ShowMessage(string message, MessageBoxType messageBoxType = MessageBoxType.With_Ok, bool autoClose = false)
        {
            bool result = false;
            
            LogHelper.Info(message);

            Application.Current.Dispatcher.Invoke(() =>
            {
                MessageBoxWindow messageBoxWindow = new MessageBoxWindow();
                messageBoxWindow.messageBoxType = messageBoxType;
                messageBoxWindow.SetHintMessage(message, autoClose);

                WindowShowAction?.Invoke(true);
                result = (bool)messageBoxWindow.ShowDialog();
                WindowShowAction?.Invoke(false);

            });

            return result;
        }
    }
}
